﻿using System.Collections.Generic;

namespace Red.Scripts.Project.Generator
{
    public class CommandLine
    {
        private Dictionary<string, string> _parameters;

        public string this[ string key ]
        {
            get { return _parameters[ key ]; }
        }

        public CommandLine( string[] args )
        {
            _parameters = Parse( args );
        }

        public bool HasParameter( string key )
        {
            return _parameters.ContainsKey( key );
        }

        public bool TryGetValue( string key, out string value )
        {
            return _parameters.TryGetValue( key, out value );
        }

        private static Dictionary<string, string> Parse( string[] args )
        {
            Dictionary<string, string> commandLineParams = new Dictionary<string, string>();

            foreach ( string arg in args )
            {
                string[] param = arg.Split( '=' );

                string key = param[ 0 ].TrimStart( new char[] { '-', '/' } );

                if ( param.Length > 1 )
                {
                    commandLineParams[ key ] = param[ 1 ];
                }
                else
                {
                    commandLineParams.Add( key, null );
                }
            }

            return commandLineParams;
        }
    }
}
